//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    flag: false,
    codeDis: false,
    phoneCode: "获取验证码",
    telephone: "",
    codePhone: ""
  },
  changeCode() {
    var _this = this
    let telephone = this.data.telephone
    var reg = 11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/;
    if (telephone.length != 11 || isNaN(telephone) || !reg.test(telephone)) {
      wx.showToast({
        title: '请输入有效的手机号码',
        icon: "none"
      })
      setTimeout(function () {
        wx.hideToast()
      }, 2000)
      return
    }
    this.setData({
      codeDis: true
    })
    //发送短信验证码
    wx.request({
      url: getApp().globalData.url + "/app/user/sendCaptcha",
      data: {
        mobile: telephone,
        sessionId: app.globalData.sessionId,
        debug: "true"
      },
      method: 'POST',
      header: { "content-type": "application/x-www-form-urlencoded" },
      success: function (res) {
        console.log(res)
        let data = res.data
        if (data.code != 0) {
          _this.setData({
            codeDis: false
          })
          wx.showToast({
            title: res.data.errmsg,
            icon: "none"
          })
          setTimeout(function () {
            wx.hideToast()
          }, 2000)
        } else {
          _this.setData({
            phoneCode: 60
          })
          let time = setInterval(() => {
            let phoneCode = _this.data.phoneCode
            phoneCode--
            _this.setData({
              phoneCode: phoneCode
            })
            if (phoneCode == 0) {
              clearInterval(time)
              _this.setData({
                phoneCode: "获取验证码",
                flag: true,
                codeDis: false
              })
            }
          }, 1000)
        }
      }
    })
  },
  phoneinput(e) {
    console.log(e)
    let value = e.detail.value
    console.log(value)
    this.setData({
      telephone: value
    })
  },
  codeinput(e) {
    let value = e.detail.value
    console.log(value)
    this.setData({
      codePhone: value
    })
  },
  close() {
    wx.navigateTo({
      url: '/pages/itemlist/itemlist'
    })
  },
  sentphone() {
    var _this = this
    const requestTask = wx.request({
      method: "POST",
      header: { "content-type": "application/x-www-form-urlencoded" },
      data: {
        mobile: _this.data.telephone,
        captcha: _this.data.codePhone,
        sessionId: app.globalData.sessionId
      },
      url: getApp().globalData.url + "/app/user/bindMobile",
      async: true,
      timeout: 6000,
      success: function (data) {
        console.log(data)
        if (data.data.code == 0) {
          wx.switchTab({
            url: '/pages/itemlist/itemlist'
          })
        } else {
          wx.showToast({
            title: data.data.errmsg,
            icon: "none"
          })
        }
      },
      error: function (textStatus) {
        console.error(textStatus);
      },
      complete: function (XMLHttpRequest, status) {
        if (status == 'timeout') {
          requestTask.abort()    // 超时后中断请求
          wx.showModal({
            title: '提示',
            content: '网络超时，请重试',
            showCancel: 'false',
                  success: function (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '/pages/index/index'
                })
              }
            }
          })
        }
      }
    })
  }
})